package com.heima.common.exception;


import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice//表名这是一个全局异常类
@Slf4j
public class ExceptionCatch {

    @ExceptionHandler(Exception.class)//处理全部Exception及其子类异常
    public ResponseResult exceptionHandler(Exception e) {
        e.printStackTrace();//控制台打印
        log.error("统一捕获异常:   {}", e);//打印日志
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "服务器出现未知错误，请稍后重试");
    }

    @ExceptionHandler(CustomException.class)//捕获自定义异常
    public ResponseResult customExceptionHandler(CustomException e) {
        e.printStackTrace();
        log.error("自定义异常：" + e);
        return ResponseResult.errorResult(e.getAppHttpCodeEnum(), e.getMessage());
    }
}
